--[[
    二级菜单
    数据结构：
        {
            name = "xxxx",
            redPoint = nil, (保留字段 如果有红点需求)
            children = {
                {name = "xxx2"},
                {name = "xxx3"}
            }
        }
    实例：
        self._menuList = SecondLevelListMenu.new(self._ui["Panel/ScrollView1"], {
            focusChangeCallback = function(d)
                dump(d, "+++++++>>> ")
            end,
            firstSelect = 1
        })
        self._menuList:reloadMenuData(self._data)

        --参数说明：
            focusChangeCallback 选择项发生改变的回调
            isAutoSelect 自动选择目录下面的第一个 默认为true
            isSingleExtend 只打开一个扩展目录
    方法：
        reloadMenuData 加载menu数据 
            params:
                firstSelect 一级菜单选择索引
                secondSelect 二级菜单选择索引
        setSelected 选择目录
]]  


local SecondLevelListMenu = class("SecondLevelListMenu")
local ListViewVertical = require("app.widget.listview.ListViewVertical")
local SecondLevelMenuFirstRender = import(".renders.SecondLevelMenuFirstRender")
local SecondLevelMenuSecondRender = import(".renders.SecondLevelMenuSecondRender")


function SecondLevelListMenu:ctor(scrollview, params)
    if not params then
        params = {}
    end
    local cellConfigs = params.cellConfigs
    if not cellConfigs then
        cellConfigs = {[1] = {renderClass = SecondLevelMenuFirstRender, perNum = 1}, [2] = {renderClass = SecondLevelMenuSecondRender, perNum = 1}}
    end

    local tagMarkFunc = params.cellTagMarkFunc
    if not tagMarkFunc then
        tagMarkFunc = function(d)
            return d.depth
        end
    end

    -- 点击 默认选择第一个
    if params.isAutoSelect == false then
        self._isAutoSelect = false
    else
        self._isAutoSelect = true
    end

    --
    if params.isSingleExtend == false then
        self._isSingleExtend = false
    else
        self._isSingleExtend = true
    end


    local listviewParams = {
        cellConfigs = cellConfigs,
        perNum = 1,
        cellTagMarkFunc = tagMarkFunc,
        cellEventProxy = handler(self, self.onCellClick)
    }
    self._listview = ListViewVertical.new(scrollview, listviewParams)
    self._focusChangeCallback = params.focusChangeCallback
    


    -- self._redPointCallback = params.redPointCallback
    self._shareData = {
        firstSelect = nil,
        secondSelect = nil,
        extends = {},
    }
    self._listview:setCellsShareData(self._shareData)
end


function SecondLevelListMenu:_handleSelected(firstSelect, secondSelect, isForce)
    if not self._menuData then
        return
    end
    if not isForce and self._shareData.firstSelect == firstSelect and self._shareData.secondSelect == secondSelect then
        return
    end

    local isExtendChange = false
    local isSelectChange = false

    if firstSelect then
        local firstSelectedData = self._menuData[firstSelect]
        if not firstSelectedData then
            return
        end

        local hasChildren = firstSelectedData.children and #firstSelectedData.children > 0

        -- 需要扩展
        if hasChildren then
            if not self._shareData.extends[firstSelect] then
                if self._isSingleExtend then
                    self._shareData.extends = {}
                end
                self._shareData.extends[firstSelect] = true
                isExtendChange = true
            end
     
            if self._isAutoSelect then
                if not secondSelect and firstSelectedData.children and #firstSelectedData.children > 0 then
                    secondSelect = 1
                    self._shareData.extends[firstSelect] = true
                    isExtendChange = true
                end
            end
        end


        if secondSelect then
            isSelectChange =  self._shareData.secondSelect ~= secondSelect
            self._shareData.firstSelect = firstSelect
            if firstSelectedData.children then
                local secondSelectedData =  firstSelectedData.children[secondSelect]
                if secondSelectedData then
                    self._shareData.secondSelect = secondSelect
                else
                    self._shareData.secondSelect = nil
                end
            else
                self._shareData.secondSelect = nil
            end 
        else
            isSelectChange = self._shareData.firstSelect ~= firstSelect
            self._shareData.firstSelect = firstSelect
            self._shareData.secondSelect = nil
        end
    end
    return isExtendChange, isSelectChange
end


-- 外部 调用
function SecondLevelListMenu:setSelected(firstSelect, secondSelect)
    local isExtendChange, isSelectChange = self:_handleSelected(firstSelect, secondSelect)
    if isExtendChange then
        self:reloadMenuData(self._menuData, {offsetY = self._listview:getTopOffsetYByIndex(firstSelect), keepIndex = firstSelect, isSameData = true})
    else
        self._listview:notifyCellsShareDataChange()
    end
    if isSelectChange then
        self:_notifySelectChange()
    end
end

function SecondLevelListMenu:onCellClick(eventName, data)
    if not data then
        return
    end
    if eventName == "clickFirstRender" then
        local menuData = data.data
        if menuData.children and #menuData.children > 0 then
            local targetExtend = not self._shareData.extends[data.depthIndex]
            local isSelectChange = false
            if targetExtend then
                -- 扩展开 
                if self._isSingleExtend then
                    self._shareData.extends = {}
                end
                self._shareData.extends[data.depthIndex] = true
                if self._isAutoSelect then
                    self._shareData.firstSelect = data.depthIndex
                    self._shareData.secondSelect = 1
                    -- 这里 会触发 选择变更
                    isSelectChange = true
                end
                self:reloadMenuData(self._menuData, {offsetY = self._listview:getTopOffsetYByIndex(data.index), keepIndex = data.depthIndex, isSameData = true})
            else
                self._shareData.extends[data.depthIndex] = false
                self:reloadMenuData(self._menuData, {offsetY = self._listview:getTopOffsetYByIndex(data.index), keepIndex = data.depthIndex, isSameData = true})
            end
            if isSelectChange then
                self:_notifySelectChange()
            end
        else
            if data.depthIndex == self._shareData.firstSelect then
                return
            end
            if self._isSingleExtend and table.nums(self._shareData.extends) > 0 then
                self._shareData.extends = {}
                self._shareData.firstSelect = data.depthIndex
                self._shareData.secondSelect = nil
                self:reloadMenuData(self._menuData, {offsetY = self._listview:getTopOffsetYByIndex(data.index), keepIndex = data.depthIndex, isSameData = true})
            else
                self._shareData.firstSelect = data.depthIndex
                self._shareData.secondSelect = nil
                self._listview:notifyCellsShareDataChange()
            end
            self:_notifySelectChange() 
        end
    elseif eventName == "clickSecondRender" then
        if data.firstDepthIndex == self._shareData.firstSelect then
            if data.depthIndex == self._shareData.secondSelect then
                return
            end
            self._shareData.secondSelect = data.depthIndex
            self._listview:notifyCellsShareDataChange()
        else
            self._shareData.firstSelect = data.firstDepthIndex
            self._shareData.secondSelect = data.depthIndex
            self._listview:notifyCellsShareDataChange()
        end
        self:_notifySelectChange()
    end
end

function SecondLevelListMenu:_notifySelectChange()
    if not self._menuData then
        return
    end
    local firstSelect = self._shareData.firstSelect
    local secondSelect = self._shareData.secondSelect
    if firstSelect then
        local firstSelectedData = self._menuData[firstSelect]
        if not firstSelectedData then
            return
        end
        if secondSelect then
            if firstSelectedData.children then
                local secondSelectedData =  firstSelectedData.children[secondSelect]
                if secondSelectedData then
                    if self._focusChangeCallback then
                        self._focusChangeCallback(secondSelectedData, firstSelect, secondSelect)
                    end
                else
                    assert(false, "secondSelectedData is nil")
                end
            end
        else
            if self._focusChangeCallback then
                self._focusChangeCallback(firstSelectedData, firstSelect)
            end
        end
    end
end

-- 获取 
function SecondLevelListMenu:reloadMenuData(menuData, params)
    self._menuData = menuData
    local listData = {}
    local index = 0
    if not params then
        params = {}
    end
    local isSelectChange = false
    local isExtendChange = false

    
    if params.firstSelect then
        isExtendChange, isSelectChange = self:_handleSelected(params.firstSelect, params.secondSelect, not params.isSameData)
    elseif not params.isSameData then
        -- 非相同的  menu 数据  强制 检查索引
        isExtendChange, isSelectChange = self:_handleSelected(self._shareData.firstSelect, self._shareData.secondSelect, true)
    end
    
    local startIndex
    local offsetY = params.offsetY or 0
    local keepIndex = params.keepIndex
    local extends = self._shareData.extends

    local firstSelect = self._shareData.firstSelect
    local secondSelect = self._shareData.secondSelect

    local foucsIndex -- 确保选中的 index 在可视范围里面
    for k, v in ipairs(self._menuData) do
        local hasChildren = v.children and #v.children > 0
        index = index + 1
        if keepIndex == k then
            startIndex = index
        end

        if firstSelect == k and not secondSelect then
            foucsIndex = index
        end

        if hasChildren then 
            local isExtend = extends[k]
            local parent = {data = v, index = index, depth = 1, depthIndex = k}
            table.insert(listData, parent)
            if isExtend then
                for k2, v2 in ipairs(v.children) do
                    index = index + 1
                    if not foucsIndex and firstSelect == k and secondSelect == k2 then
                        foucsIndex = index
                    end
                    table.insert(listData, {data = v2, index = index, depth = 2, depthIndex = k2, firstDepthIndex = k})
                end
            end
        else
            table.insert(listData, {data = v, index = index, depth = 1, depthIndex = k})
        end
    end

    self._listview:reloadData(listData, {
        startIndex = startIndex,
        startIndexOffset = offsetY
    })

    if isSelectChange then
        self:_notifySelectChange()
    end

    -- 检查选中的 是否在可视范围内
    if foucsIndex then
        self._listview:scrollToVisibleArea(foucsIndex)
    end

end

return SecondLevelListMenu